<template>
  <div class="lc_wrap">
    <el-row class="HistorydataTabel">

      <!-- 左侧树 -->
      <el-col :span="leftWidth">
        <div class="box treeBox">
          <basic-container>
            <lcTree @ready="ready" :deviceType="1"></lcTree>
          </basic-container>
        </div>
      </el-col>

      <!-- 内容 -->
      <el-col :span="rightWidth" class="table-wrapper">
        <overlay @changeTreeBox="changeTreeBox"></overlay>
        <basic-container>

          <!-- 自定义搜索功能 -->
          <div class="search-tools">
            <div class="search-left">

              <!-- 开始日期 -->
              <el-date-picker
                v-model="dateFrom"
                type="date"
                size="small"
                placeholder="开始日期"
                :picker-options="pickerOption"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
              <label class="search_label">至</label>

              <!-- 结束日期 -->
              <el-date-picker
                v-model="dateTo"
                type="date"
                size="small"
                placeholder="结束日期"
                :picker-options="pickerOption"
                value-format="yyyy-MM-dd"
              ></el-date-picker>

              <!-- 搜索 -->
              <el-button 
                class="search_btn" 
                type="primary" 
                size="small" 
                icon="el-icon-search" 
                @click="searchBtn">
                搜 索
              </el-button>

            </div>
          </div>

          <!-- 表格内容 -->
          <avue-crud
            :option="option"
            :table-loading="loading"
            :data="data"
            v-model="form"
            ref="crud"
            :page="page"
            @current-change="currentChange"
            @size-change="sizeChange"
          >

            <!-- 操作按钮 -->
            <template slot-scope="scope" slot="menu">
              <el-button 
                type="text" 
                icon="el-icon-edit" 
                size="small" 
                @click.stop="handleAbnormal(scope.row, 'edit')" 
                :disabled="!scope.row['beHandle']">
                处理
              </el-button>
              <el-button 
                type="text" 
                icon="el-icon-view" 
                size="small" 
                @click.stop="handleAbnormal(scope.row, 'view')">
                查看
              </el-button>
            </template>

          </avue-crud>
        </basic-container>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getList } from "@/api/loam/handleAbnormal";
import { mapGetters } from "vuex";
import { dateFormat } from "@/util/date";
import lcTree from "@/components/common/tree.vue";
import overlay from "@/components/common/overlay.vue";

export default {
  components: {
    lcTree, 
    overlay
  },
  data() {

    // 定义一个时间格式化函数
    Date.prototype.format = function(format) {
      var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(), 
        "s+": this.getSeconds(), 
        "q+": Math.floor((this.getMonth() + 3) / 3),
        S: this.getMilliseconds() 
      };
      if (/(y+)/.test(format)) {
        format = format.replace(
          RegExp.$1,
          (this.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      }
      for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
          format = format.replace(
            RegExp.$1,
            RegExp.$1.length == 1
              ? o[k]
              : ("00" + o[k]).substr(("" + o[k]).length)
          );
        }
      }

      return format;
    };

    return {
      leftWidth: this.$leftWidth,
      rightWidth: this.$rightWidth,
      form: {},
      loading: false,
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        refreshBtn: false,
        header: false,
        dialogClickModal: false,
        editBtn: false,
        delBtn: false,
        index:true,
        column: [{
          label: "企业",
          prop: "deptName",
          align: "center"
        }, {
          label: "日期",
          prop: "reportDate",
          align: "center"
        }, {
          label: "异常数量",
          prop: "num",
          align: "center"
        // }, {
        //   label: "督办单号",
        //   prop: "reportNo",
        //   align: "center"
        }, {
          label: "提交人",
          prop: "createUserName",
          align: "center"
        }, {
          label: "当前节点",
          prop: "taskName",
          align: "center"
        }]
      },
      data: [],
      deptInfoId: "",
      nodeId: "",
      pickerOption: {
        firstDayOfWeek: 1,
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      dateFrom: this.fun_date(-30),
      dateTo: dateFormat(new Date(), "yyyy-MM-dd"),
      preDateFrom: this.fun_date(-30),
      preDateTo: dateFormat(new Date(), "yyyy-MM-dd")
    };
  },
  computed: {
    ...mapGetters(["permission"])
  },
  watch: {

    // 开始日期
    dateFrom() {
      if (this.dateFrom) {
        let startDate = new Date(this.dateFrom);
        let endDate = new Date(this.dateTo);
        if (startDate.getTime() > endDate.getTime()) {
          this.dateTo = this.dateFrom;
        } else if (endDate.getTime() > startDate.getTime() + 30 * 24 * 60 * 60 * 1000) {
          startDate.setTime(startDate.getTime() + 30 * 24 * 60 * 60 * 1000);
          this.dateTo = dateFormat(new Date(startDate), "yyyy-MM-dd");
        }
      }
    },
    
    // 结束日期
    dateTo() {
      if (this.dateTo) {
        let startDate = new Date(this.dateFrom);
        let endDate = new Date(this.dateTo);
        if (startDate.getTime() > endDate.getTime()) {
          this.dateFrom = this.dateTo;
        } else if (endDate.getTime() - 30 * 24 * 60 * 60 * 1000 > startDate.getTime()) {
          endDate.setTime(endDate.getTime() - 30 * 24 * 60 * 60 * 1000);
          this.dateFrom = dateFormat(new Date(endDate), "yyyy-MM-dd");
        }
      }
    }
  },
  methods: {

    // 收缩、显示左侧树
    changeTreeBox() {
      this.leftWidth = this.leftWidth == this.$leftWidth ? 0 : this.$leftWidth
      this.rightWidth = this.rightWidth == this.$rightWidth ? 24 : this.$rightWidth
    },

    // 左侧树加载事件
    ready(data) {
      this.nodeId = data.key;
      this.deptInfoId = data.id;
      this.dateFrom = this.fun_date(-30);
      this.dateTo = dateFormat(new Date(), "yyyy-MM-dd");
      this.preDateFrom = this.dateFrom;
      this.preDateTo = this.dateTo;
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },

    // 搜索
    searchBtn() {
      this.preDateFrom = this.dateFrom;
      this.preDateTo = this.dateTo;
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },

    // 页码改变
    currentChange(currentPage) {
      this.dateFrom = this.preDateFrom;
      this.dateTo = this.preDateTo;
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 每页条数改变
    sizeChange(pageSize) {
      this.dateFrom = this.preDateFrom;
      this.dateTo = this.preDateTo;
      this.page.pageSize = pageSize;
      this.onLoad(this.page)
    },

    // 获取表格内容
    onLoad(page) {
      var deptId = this.deptInfoId;
      var start = this.preDateFrom;
      var end = this.preDateTo;
      var current = page.currentPage;
      var size = page.pageSize;
      getList(deptId, start, end, current, size).then(res => {
        if(res.data.success !== true) {
          return;
        }
        var resData = res.data.data;

        this.data = resData.records;
        this.page.total = resData.total;
        this.loading = false;
      });
    },

    // 补全时间
    formatNumber(num) {
      if(num < 10) {
        return "0" + num;
      }
      return num;
    },

    // 获取日期
    fun_date(num) {
      var date1 = new Date();
      var date2 = new Date( date1 );
      date2.setDate(date1.getDate() + num);
      var year = date2.getFullYear();
      var month = date2.getMonth() + 1;
      var day = date2.getDate();
      var time2 = year + "-" + this.formatNumber(month) + "-" + this.formatNumber(day);
      return time2;
    },

    // 处理按钮
    handleAbnormal(row, type) {
      var query = {
        nodeId: this.nodeId,
        deptInfoId: row.deptInfoId,
        deptName: row.deptName,
        id: row.id,
        processInstanceId: row.processInstanceId || "-",
        type: type,
        taskKey: row.taskKey
      };
      sessionStorage.setItem('abnormalReport', JSON.stringify(query));
      this.$router.push({ 
        path: '/abnormalReport',
      });
    }

  }
};
</script>


<style lang="scss" scoped>
.map {
  width: 100%;
  height: 500px;
}

.box {
  height: 100%;
  margin-bottom: 0;
}

.el-row {
  height: calc(100% - 20px);
}

.el-col {
  height: 100%;
  overflow: auto;
}

.search-tools {
  height: 49px;
  display: flex;
  justify-content: space-between;
  padding: 0 8px;
}

.search-left {
  display: flex;
  height: 33px;
  line-height: 33px;
}

.search-right {
  display: flex;
}

.search-tabs {
  height: 33px;
  display: flex;
  align-items: center;
  margin-right: 40px;
}

.overlay {
  position: absolute;
  width: 200px;
  height: calc(100% - 35px);
  opacity: 0;
  display: flex;
  align-items: center;
  padding-left: 6px;
  .tree-tool {
    height: 34px;
    width: 18px;
    background: #fff;
    box-shadow: 2px 1px 4px #ccc;
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
    cursor: pointer;
    display: flex;
    padding: 6px 0;
    z-index: 9999;
    .tool-line {
      margin-left: 3px;
      height: 100%;
      border-left: 1px solid #ddd;
    }
  }
}

.overlay:hover {
  opacity: 1;
}

.search_label{
  font-size: 14px;
  margin: 0px 10px;
}

.search_btn{
  margin-left: 10px;
}
</style>